%option noyywrap
%option noinput

%{
#include <string.h>
#include "grammar.h"
#include "checker.h"
%}

LBRACE		[\{]
RBRACE		[\}]
LSQUARE		[\[]
RSQUARE		[\]]
COMMA		[,]
COLON		[:]
BOOL		true|false|null
CHAR		[\x00-\x21\x23-\x5b\x5d-\xff]
QUOTES		[\"]
RSOLIDUS	[\\]
SPECIAL		[\"\\\/bfnrtu]
POINT		[\.]
EXP			[eE]
MINUS		[-]
EXPSIGN		[-+]
DIGIT		[0-9]
SPACE		" "
NEWLINE		[\n]

%x string
%x escape
%x number

%%

%{
char *ch;
token_node *node;
%}

<INITIAL>{LBRACE}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, "{");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = LBRACE;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														node->value = ch;
														zzlval.node = node;
														return LBR;}
<INITIAL>{RBRACE}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, "}");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = RBRACE;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return RBR;}
<INITIAL>{LSQUARE}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, "[");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = LSQUARE;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return LSQ;}
<INITIAL>{RSQUARE}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, "]");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = RSQUARE;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return RSQ;}
<INITIAL>{COMMA}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, ",");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = COMMA;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return COM;}
<INITIAL>{COLON}										{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, ":");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = COLON;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return COL;}
<INITIAL>{BOOL}											{ch = (char*) malloc(sizeof(char)*8);
														strcpy(ch, zztext);
														node = (token_node*) malloc(sizeof(token_node));
														node->token = BOOL;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return BOO;}
<INITIAL>{QUOTES}										{BEGIN(string);
														ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, "\"");
														node = (token_node*) malloc(sizeof(token_node));
														node->token = QUOTES;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return QUO;}
<string>{CHAR}											{ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, zztext);
														node = (token_node*) malloc(sizeof(token_node));
														node->token = CHAR;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return CHA;}
<string>{RSOLIDUS}										{BEGIN(escape);
														yymore();}
<string>{QUOTES}										{BEGIN(INITIAL);
														ch = (char*) malloc(sizeof(char)*2);
														strcpy(ch, zztext);
														node = (token_node*) malloc(sizeof(token_node));
														node->token = QUOTES;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return QUO;}
<escape>{SPECIAL}										{BEGIN(string);
														ch = (char*) malloc(sizeof(char)*3);
														strcpy(ch, zztext);
														node = (token_node*) malloc(sizeof(token_node));
														node->token = CHAR;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														return CHA;}
<INITIAL>{DIGIT}|{MINUS}								{yymore();
														BEGIN(number);}
<number>{DIGIT}|{EXP}|{EXPSIGN}|{POINT}					{yymore();}
<number>{COMMA}|{SPACE}|{RBRACE}|{RSQUARE}				{ch = (char*) malloc(sizeof(char)*strlen(zztext));
														strcpy(ch, zztext);
														ch[strlen(zztext) - 1] = '\0';
														unput(zztext[zzleng - 1]);
														node = (token_node*) malloc(sizeof(token_node));
														node->token = NUMBER;
														node->value = ch;
                                                        node->next = NULL;
                                                        node->parent = NULL;
                                                        node->child = NULL;
														zzlval.node = node;
														BEGIN(INITIAL);
														return NUM;}
<INITIAL>{SPACE}										{}
<INITIAL>{NEWLINE}										{}


%%



